package ${initProjectVo.packageName}.controller;

import ${initProjectVo.packageName}.service.IRoleUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${initProjectVo.packageName}.entity.UserInfo;
import ${initProjectVo.packageName}.service.IUserInfoService;
import ${initProjectVo.packageName}.utils.SessionUtils;
import ${initProjectVo.packageName}.vo.UserInfoVo;
import ${initProjectVo.packageNameValue}.core.controller.BaseController;
import ${initProjectVo.packageNameValue}.core.result.ResultData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
* @author Faye.Wang
* @version 1.0
* @date 2021/1/6 14:03
* @Description
*/
@RestController
@RequestMapping("/api/user")
public class UserInfoController extends BaseController {

	@Autowired
	private IUserInfoService iUserInfoService;

	@Autowired
	private IRoleUserService iRoleUserService;

	@Value("${"$"}{sys.init.pass}")
	private String initPass;

	/**
	* 保存数据
	* @param record
	* @return
	*/
	@PostMapping("/insert")
	public ResultData insertUserInfo(@RequestBody UserInfoVo record){
		if(iUserInfoService.loadUserInfoByName(record.getUserName())!=null){
			return super.resultSuccess("用户名以存在，请更换",null);
		}else{
			UserInfo entity = new UserInfo();
			BeanUtils.copyProperties(record,entity);
			entity.setUsername(record.getUserName());
			entity.setPassword(new BCryptPasswordEncoder().encode(initPass));
			entity.setEnable(0);
			entity.setCreateTime(new Date());
			iUserInfoService.insertUser(entity);
			return super.resultSuccess();
		}
	}
	/**
	* 修改数据
	* @param record
	* @return
	*/
	@PostMapping("/update")
	public ResultData updateUser(@RequestBody UserInfoVo record){
		UserInfo entity = new UserInfo();
		BeanUtils.copyProperties(record,entity);
		iUserInfoService.updateUserProfile(entity);
		return super.resultSuccess();
	}


	/**
	* 删除数据
	* @param record
	* @return
	*/
	@PostMapping("/delete")
	public ResultData deleteUserById(@RequestBody UserInfoVo record){
		iUserInfoService.deletePrimaryKey(record.getId());
		return super.resultSuccess();
	}

	/**
	* 修改密码
	* @param userInfoVo 用户信息
	* @return
	*/
	@PostMapping("/updateUserPass")
	public ResultData updateUserPass(@RequestBody UserInfoVo userInfoVo){
		UserInfo userInfo = new UserInfo();
		userInfo.setPassword(new BCryptPasswordEncoder().encode(userInfoVo.getPassWord()));
		userInfo.setId(SessionUtils.getSessionUserId(userInfoVo.getSessionId()));
		iUserInfoService.updateUserProfile(userInfo);
		return super.resultSuccess();
	}
	/**
	* 获取用户权限与名称
	* @param request
	* @return
	*/
	@PostMapping("/getUserInfo")
	public ResultData getUserInfo(HttpServletRequest request){
		String sessionId = SessionUtils.getTokenForUserSessionId(request);
		Integer userId = SessionUtils.getSessionUserId(sessionId);
		String[] roles =  iRoleUserService.selectUserRole(userId);
		String userName = SessionUtils.getSessionUserName(sessionId);
		Map<String,Object> dataMap = new HashMap<>();
		dataMap.put("roles",roles);
		dataMap.put("userName",userName);
		return super.resultSuccess(dataMap);
	}
	/**
	* 分页查询数据
	* @param record
	* @return
	*/
	@PostMapping("/getPageQuery")
		public ResultData getPageQuery(@RequestBody UserInfoVo record){
		UserInfo entity = new UserInfo();
		BeanUtils.copyProperties(record,entity);
		return super.resultSuccess(iUserInfoService.selectEntityList(new Page<>(record.getPageNo(), record.getPageSize()),entity));
	}

	/**
	* get user's information and user's role name
	* @param record
	* @return
	*/
	@PostMapping("/getUserById")
	public ResultData getUserById(@RequestBody UserInfoVo record){
		return super.resultSuccess(iUserInfoService.getUserEntityById(record.getId()));
	}
}
